package com.cheng.im.service.massage.service.impl;

import com.cheng.im.service.common.eunms.*;
import com.cheng.im.service.common.resp.CommonResult;
import com.cheng.im.service.friendship.service.ImFriendshipService;
import com.cheng.im.service.frienship.domain.ImFriendship;
import com.cheng.im.service.frienship.model.req.GetRelationReq;
import com.cheng.im.service.group.domain.ImGroup;
import com.cheng.im.service.group.model.resp.GetRoleInGroupResp;
import com.cheng.im.service.group.service.ImGroupMemberService;
import com.cheng.im.service.group.service.ImGroupService;
import com.cheng.im.service.message.service.CheckSendMessageService;
import com.cheng.im.service.user.domain.ImUserData;
import com.cheng.im.service.user.service.ImUserDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
@Slf4j
public class CheckSendMessageServiceImpl implements CheckSendMessageService {

    @Autowired
    ImUserDataService imUserDataService;
    @Autowired
    ImFriendshipService imFriendshipService;
    @Autowired
    ImGroupService imGroupService;
    @Autowired
    ImGroupMemberService imGroupMemberService;

    @Override
    public CommonResult checkFriendShip(String fromId, String toId, String appId) {
        // log.info("===========检查好友关系=============");
        GetRelationReq fromReq = new GetRelationReq();
        fromReq.setFromId(fromId);
        fromReq.setToId(toId);
        fromReq.setAppId(appId);
        CommonResult<ImFriendship> fromRelation = imFriendshipService.getRelation(fromReq);
        if(!fromRelation.isOk()){
            return fromRelation;
        }

        fromReq.setFromId(toId);
        fromReq.setToId(fromId);
        fromReq.setAppId(appId);
        CommonResult<ImFriendship> toRelation = imFriendshipService.getRelation(fromReq);
        if(!toRelation.isOk()){
            return toRelation;
        }

        if(FriendShipStatusEnum.FRIEND_STATUS_NORMAL.getCode()
                != fromRelation.getData().getStatus()){
            return CommonResult.failed(FriendShipErrorCode.FRIEND_IS_DELETED);
        }

        if(FriendShipStatusEnum.FRIEND_STATUS_NORMAL.getCode()
                != toRelation.getData().getStatus()){
            return CommonResult.failed(FriendShipErrorCode.FRIEND_IS_DELETED);
        }

        // 黑名单
        if(FriendShipStatusEnum.BLACK_STATUS_NORMAL.getCode()
                != fromRelation.getData().getBlack()){
            return CommonResult.failed(FriendShipErrorCode.FRIEND_IS_BLACK);
        }

        if(FriendShipStatusEnum.BLACK_STATUS_NORMAL.getCode()
                != toRelation.getData().getBlack()){
            return CommonResult.failed(FriendShipErrorCode.TARGET_IS_BLACK_YOU);
        }
        return CommonResult.success();
    }

    @Override
    public CommonResult checkSenderForvidAndMute(String fromId, String appId) {
        CommonResult<ImUserData> singleUserInfo
                = imUserDataService.getSingleUserInfo(fromId, appId);
        if(!singleUserInfo.isOk()){
            return singleUserInfo;
        }

        ImUserData user = singleUserInfo.getData();
        if(user.getForbiddenFlag() == UserForbiddenFlagEnum.FORBIBBEN.getCode()){
            return CommonResult.failed(MessageErrorCode.FROMER_IS_FORBIBBEN);
        }else if (user.getSilentFlag() == UserSilentFlagEnum.MUTE.getCode()){
            return CommonResult.failed(MessageErrorCode.FROMER_IS_MUTE);
        }

        return CommonResult.success();
    }

    @Override
    public CommonResult checkGroupMessage(String fromId, String groupId, String appId) {
        CommonResult responseVO = checkSenderForvidAndMute(fromId, appId);
        if(!responseVO.isOk()){
            return responseVO;
        }

        //判断群逻辑
        CommonResult<ImGroup> group = imGroupService.getGroup(groupId, appId);
        if(!group.isOk()){
            return group;
        }

        //判断群成员是否在群内
        CommonResult<GetRoleInGroupResp> roleInGroupOne = imGroupMemberService.getRoleInGroupOne(groupId, fromId, appId);
        if(!roleInGroupOne.isOk()){
            return roleInGroupOne;
        }
        GetRoleInGroupResp data = roleInGroupOne.getData();

        //判断群是否被禁言
        //如果禁言 只有裙管理和群主可以发言
        ImGroup groupData = group.getData();
        if(groupData.getMute() == GroupMuteTypeEnum.MUTE.getCode()
                && (data.getRole() == GroupMemberRoleEnum.MAMAGER.getCode() ||
                data.getRole() == GroupMemberRoleEnum.OWNER.getCode()  )){
            return CommonResult.failed(GroupErrorCode.THIS_GROUP_IS_MUTE);
        }

        if(data.getSpeakDate() != null && data.getSpeakDate() > System.currentTimeMillis()){
            return CommonResult.failed(GroupErrorCode.GROUP_MEMBER_IS_SPEAK);
        }

        return CommonResult.success();
    }
}
